<?php

namespace App\Admin\Actions;

use App\Exceptions\BizException;
use App\Services\RuleService;
use Encore\Admin\Actions\Action;
use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class GetBairongData extends RowAction
{
    public $name = '获取百融数据';

    public function handle($model, Request $request)
    {

        try {
            $redis_key = 'withdrawal_lock'.$model->id;
            if(!Redis::set($redis_key, 1, 'ex', 30, 'nx')) {
                throw new BizException('操作太频繁');
            }
            $rule_service = new RuleService();
            $rule_service->getAllBairong($model);

        } catch (\Exception $e) {
            Redis::expire($redis_key,2);
            return $this->response()->error($e->getMessage());
        }
        Redis::expire($redis_key,2);
        return $this->response()->success('操作成功')->refresh();
    }

    public function dialog()
    {
        $this->confirm('确定获取百融全部数据吗？');
    }



    public function authorize($user, $model)
    {
        return true;
    }
}
